package com.labManage.controller;

import com.labManage.dao.PermitionDao;
import com.labManage.dao.RolePermitionDao;
import com.labManage.entity.Permition;
import com.labManage.service.PermitionService;
import com.labManage.vo.ResultVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.rmi.runtime.Log;

import java.util.List;

@Controller
@RequestMapping("/permition")
public class PermitionController {

    @Autowired
    private PermitionDao permitionDao;

    @Autowired
    private PermitionService permitionService;
    @Autowired
    private RolePermitionDao rolePermitionDao;
    @RequestMapping("/getAllPermition")
    @ResponseBody
    public ResultVo getAllPermition(){
        List<Permition> parentPers = permitionDao.selectParentPers();
        return ResultVo.build(0,"",parentPers);
    }

    @RequestMapping("/getPermitionByRoleId")
    @ResponseBody
    public ResultVo getPermitionByRoleId(Long roleId){
        List<Long> permitionIds = rolePermitionDao.selectPermitionByRoleId(roleId);
        if(null!=permitionIds){
            return ResultVo.build(0,"",permitionIds);
        }else{
            return ResultVo.build(201,"");
        }
    }

    @RequestMapping("/permitionList")
    @ResponseBody
    public ResultVo permitionList(){
        List<Permition> parentPers = permitionDao.selectAllPermition();
        return ResultVo.build(0,"",parentPers);
    }

    @RequestMapping("/deletePermitionInfo")
    @ResponseBody
    @RequiresPermissions("permition:delete")
    public ResultVo deletePermitionInfo(Long permitionId){
        int result = permitionService.deletePermitionInfo(permitionId);
        if(1==result){
            return ResultVo.build(201,"请删除对应子权限");
        }else if(1<result){
            return ResultVo.build(0,"删除成功");
        }else{
            return ResultVo.build(201,"");
        }
    }

    @RequestMapping("/addPermitionInfo")
    @ResponseBody
    @RequiresPermissions("permition:add")
    public ResultVo addPermitionInfo(Permition permition){
        int result = permitionService.addPermitionInfo(permition);
        if(0<result){
            return ResultVo.build(0,"添加成功");
        }else {
            return ResultVo.build(201,"添加失败");
        }
    }

    @RequestMapping("/modifyPermitionInfo")
    @ResponseBody
    @RequiresPermissions("permition:modify")
    public ResultVo modifyPermitionInfo(Permition permition){
        int result = permitionService.modifyPermitionInfo(permition);
        if(0<result){
            return ResultVo.build(0,"修改成功");
        }else {
            return ResultVo.build(201,"修改失败");
        }
    }

    @RequestMapping("/getParentPermitionByChildPermitionId")
    @ResponseBody
    public ResultVo getParentPermitionByChildPermitionId(Long childPermitionId){
        Permition permition = permitionDao.selectParentPermitionByChildPermitionId(childPermitionId);
        if(null!=permition){
            if(permition.getId() == childPermitionId){
                permition.setPermitionName("");
            }
            return ResultVo.build(0,"",permition);
        }else{
            return ResultVo.build(201,"");
        }
    }
}
